/**
 * @Title: feeView_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年9月25日 下午2:06:27
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('FeeViewCtrl',['$scope','FeeViewService','FwaTabsetService','BaseURL','$stateParams','CommunityService','Modal',
	                              function($scope,feeViewService,FwaTabsetService,baseUrl,$stateParams,CommunityService,Modal){
		$scope.URL = baseUrl;
		var endDate = $stateParams.date,
		indicator = $stateParams.indicator,
		hospType = $stateParams.hospType;
		$scope.xOpdata = [];//  x轴 data
		//y轴data
//		$scope.totalPayData = [];//医疗总费用
//		$scope.perMonthData = [];//预拨费用
		var sumFeeData = [];//费用总额
		var selFeeData = [];//自付费用
		var selFeeRat = [];//自付比例
		var chainData = []; // 环比
		var periodData = []; //同比
		
		$scope.feeInfo = {
				year : "",
				startMonth : "",
				endMonth : "",
				totalPay : "",
				preFeeTot : "",
				curMonthVal : "",
				compSamePeriod : "",
				compChain : "",
				curMonthValFee:"",
	    		compSamePeriodFee:"",
	    		compChainFee:"",
	    		curMonthValRat:"",
	    		compSamePeriodRat:"",
	    		compChainRat:""
		};
		
		//feeExeParam 费用情况linechart
		function feeExeOption (){
			return {
				grid:{
					x:45,
					y:5,
					x2:15,
					y2:5,
					borderColor:'#eee'
				},
			    tooltip : {
			        trigger: 'axis',
			        showDelay: 0
			    },
			    legend: {
			    	padding:[5],
			    	x:'center'
			    },
			    calculable : false,
			    xAxis : [
			        {
			            type : 'category',
			            boundaryGap : true,
			            data :$scope.xOpdata
			        }
			    ],
			    yAxis : [
			        {
			            type : 'value'
			        }
			    ],
			    color :['#cc99cc','#ff7878'],
			    series : $scope.feeExeSeries
			};
		}
		 
		function getFeeExeYSeries(totalPayData, perFeeData) {
			return [{
	            name:'医疗总费用',
	            type:'line',
	            yAxisIndex : 0,
	            data:totalPayData,
	            itemStyle:{
	            	normal:{
	            		areaStyle:{
	            			type:'default'
	            		}
	            	}
	            }
	        },{
	            name:'预拨费用',
	            type:'line',
	            data:perFeeData,
	            itemStyle:{
	            	normal:{
	            		lineStyle:{
	            			type:'dashed'//虚线
	            		}
	            	}
	            }
	        }];
		}
		
		//同比 环比
		function periodOrchainOption (){
			return {
				grid:{
					x:45,
					y:5,
					x2:15,
					y2:30,
					borderColor:'#eee'
				},
			    tooltip : {
			        trigger: 'axis',
			        showDelay: 0
			        	
			    },
			    legend: {
			    	padding:[5],
			    	x:'center'
			    },
			    calculable : false,
			    xAxis : [
			        {
			            type : 'category',
			            boundaryGap : true,
			            data :$scope.xOpdata
			        }
			    ],
			    yAxis : [
			        {
			            type : 'value',
			            axisLabel : {
			                formatter: '{value}%'
			            }
			        }
			    ],
			    color :['#ffa0a0','#89cbed'],
			    series : [{
		            name:'同比',
		            type:'bar',
		            data:periodData,
		            itemStyle:{
		            	normal:{
		            		areaStyle:{
		            			type:'default'
		            		}
		            	}
		            }
		        },{
		            name:'环比',
		            type:'bar',
		            data:chainData,
		            itemStyle:{
		            	normal:{
		            		areaStyle:{
		            			type:'default'
		            		}
		            	}
		            }
		        }]
			};
		}
		
		//feeHappen 费用情况linechart
		function feeHappenOption (){
			return {
				grid:{
					x:45,
					y:5,
					x2:15,
					y2:5,
					borderColor:'#eee'
				},
			    tooltip : {
			        trigger: 'axis',
			        showDelay: 0
			    },
			    legend: {
			    	padding:[5],
			    	x:'center'
			    },
			    calculable : false,
			    xAxis : [
			        {
			            type : 'category',
			            boundaryGap : true,
			            data :$scope.xOpdata
			        }
			    ],
			    yAxis : [
			        {
			            type : 'value'
			        }
			    ],
			    color :['#0c77af','#57b5e6'],
			    series : [{
		            name:'费用总额',
		            type:'line',
		            data:sumFeeData,
		            itemStyle:{
		            	normal:{
		            		areaStyle:{
		            			type:'default'
		            		}
		            	}
		            }
		        },{
		            name:'自付费用',
		            type:'line',
		            itemStyle:{
		            	normal:{
		            		areaStyle:{
		            			type:'default'
		            		}
		            	}
		            },
		            data:selFeeData
		        }]
			};
		}
		
		//自付比例
		function feeBarOption(){
			return {
				grid:{
					x:45,
					y:5,
					x2:15,
					y2:30,
					borderColor:'#eee'
				},
			    tooltip : {
			        trigger: 'axis',
			        showDelay: 0
			    },
			    legend: {
			    	padding:[5],
			    	x:'left'
			    },
			    calculable : false,
			    xAxis : [
			        {
			            type : 'category',
			            boundaryGap : true,
			            data :$scope.xOpdata
			        }
			    ],
			    yAxis : [
			        {
			            type : 'value',
			            axisLabel : {
			                formatter: '{value}%'
			            }
			        }
			    ],
			    color :['#57b5e6'],
			    series : [{
		            name:'自付比例',
		            type:'bar',
		            data:selFeeRat,
		            itemStyle:{
		            	normal:{
		            		areaStyle:{
		            			type:'default'
		            		}
		            	}
		            }
		        }]
			};
		}
		
		//费用总额  医疗总费用
		var feeExeParamChart ;
		var periodOrchainChart ;
		
		//费用执行情况
		var getFeeExeInfos = function(type){
			feeViewService.getFeeExeInfos($scope.date,$scope.typeSelected,indicator,hospType).success(function(res){
				if (res.state == "0") {
					//y 轴data
					$scope.feeExeSeries = getFeeExeYSeries(toFixedDatas(res.totalPayData),toFixedDatas(res.perMonthData));
					chainData = toFixedDatas(res.chainData);
					periodData = toFixedDatas(res.periodData);
					
					totalCostOption();
					$scope.feeInfo.totalPay = parseFloat(res.totalPay ||0).toFixed(2);
					$scope.feeInfo.preFeeTot = parseFloat(res.preFeeTot ||0).toFixed(2);
					
					$scope.feeInfo.curMonthVal = parseFloat(res.curMonthVal ||0).toFixed(2);
					
					$scope.feeInfo.perMonthCurMonthVal = parseFloat(res.perMonthCurMonthVal ||0).toFixed(2);
					$scope.feeInfo.perMonthCompChain = parseFloat(res.perMonthCompChain ||0).toFixed(2);
					$scope.feeInfo.perMonthCompSamePeriod = parseFloat(res.perMonthCompSamePeriod ||0).toFixed(2);
					
					$scope.feeInfo.compSamePeriod = parseFloat(res.compSamePeriod ||0).toFixed(2);
					$scope.feeInfo.compChain = parseFloat(res.compChain ||0).toFixed(2);
				}
				else {
					chainData = [];
					periodData = [];
					$scope.feeInfo = {};
					$scope.feeExeSeries = {};
					totalCostOption();
					if (!msg) {
						msg = res.msg;
						Modal.alert(res.msg);
					}
				}
			});
		};
		
		function totalCostOption() {
			//绘图
			feeExeParamChart = echarts.init(document.getElementById('feeExeParamId1'));
			feeExeParamChart.setOption(feeExeOption());
			
			periodOrchainChart = echarts.init(document.getElementById('periodOrchainChartId1'));
			periodOrchainChart.setOption(periodOrchainOption());
			
			//多图联动
			periodOrchainChart.connect([feeExeParamChart]);	
			feeExeParamChart.connect([periodOrchainChart]);	
		}
		
		$scope.typeSelected = "1";//默认为按期
		$scope.typeClick = function(type){
			$scope.typeSelected = type;
			getFeeExeInfos(type);
		};
		
		//费用总额 以及自付费用 绘图对象
		var feeHappenChart;
		
		//自付比例绘图对象
		var feeBarChart;
		
		//存储出错信息
		var msg = "";
		
		//根据日期查询信息
		var getChartsData = function() {
			msg = "";
			$scope.xOpdata = [];//清空	
			var start = $scope.date.start;
			var end = $scope.date.end;
			var startMonth = parseInt(start.substring(5,7)),
			endMonth = parseInt(end.substring(5,7)),
			startYear = parseInt(start.substring(0,4)),
			endYear = parseInt(end.substring(0,4));
			
			//根据选择的开始、结束月改变相应的横轴坐标
			var monthSize = ( parseInt(endYear) - parseInt(startYear)) * 12 + parseInt(endMonth) - parseInt(startMonth) + 1;
			var rightMonth=1;
			for(var j = 0; j < monthSize; j++) {
				if((parseInt(startMonth)+j) <= 12){
					$scope.xOpdata.push((parseInt(startMonth) + j) + "月");
				}else{					
					$scope.xOpdata.push(rightMonth + "月");
					rightMonth++;
				}
				
			}
			if ($scope.xOpdata == '') {
				$scope.xOpdata = ['月'];
			}
			//费用执行情况
			getFeeExeInfos($scope.typeSelected);
			
			//费用发生分析
			feeViewService.getFeeHappenAnalysis($scope.date,hospType).success(function(res){
				if (res.state == "0") {
					sumFeeData = toFixedDatas(res.sumFeeData);
					selFeeData = toFixedDatas(res.selFeeData);
					selFeeRat = toFixedDatas(res.selFeeRat);
					
					pocketCostsOption();
					$scope.feeInfo.curMonthValFee = parseFloat(res.curMonthValFee ||0).toFixed(2);
					$scope.feeInfo.compSamePeriodFee = parseFloat(res.compSamePeriodFee ||0).toFixed(2);
					$scope.feeInfo.compChainFee = parseFloat(res.compChainFee ||0).toFixed(2);
					$scope.feeInfo.curMonthValRat = parseFloat(res.curMonthValRat ||0).toFixed(2);
					$scope.feeInfo.compSamePeriodRat = parseFloat(res.compSamePeriodRat ||0).toFixed(2);
					$scope.feeInfo.compChainRat = parseFloat(res.compChainRat ||0).toFixed(2);
				}
				else {
					sumFeeData = [];
					selFeeData = [];
					selFeeRat = [];
					pocketCostsOption();
					if (!msg) {
						msg = res.msg;
						Modal.alert(res.msg);
					}
				}
			});
			
		};
		function toFixedDatas(datas) {
			var newDatas = [];
			angular.forEach(datas,function(data,index){
				newDatas.push(parseFloat(data).toFixed(2));
			});
			return newDatas;
		}
		
		//绘图开始
		function pocketCostsOption() {
			//绘图
			feeHappenChart = echarts.init(document.getElementById('feeHappenChartId1'));
			feeHappenChart.setOption(feeHappenOption());
			feeBarChart = echarts.init(document.getElementById('feeBarChartId1'));
			feeBarChart.setOption(feeBarOption());
			//多图联动
			feeHappenChart.connect([feeBarChart]);	
			feeBarChart.connect([feeHappenChart]);	
		}
		
		//时间变化处理
		$scope.$on('dateRangeInput.change',function(e,v){
			if(v) {
				var checkTemp = CommunityService.checkDateSpan(v.start,v.end);
				if	(checkTemp != ''
					&& checkTemp.length > 0) {
					Modal.alert(checkTemp);
					return false;
				}
				getChartsData();
			}
			
		});
		
		//根据结算分析日期向前推一年查询
		if (!endDate) {
			CommunityService.initDate("yyyy-MM").success(function(res) {
				endDate  = CommunityService.lastMonth(res.formatDate);
				$scope.date = {
						start:	CommunityService.lastYear(endDate),
						end: endDate
					};
				getChartsData();
			});
		}
		else {
			$scope.date = {
					start:	CommunityService.lastYear(endDate),
					end: endDate
				};
			getChartsData();
		}
		
		//查看指标
		$scope.queryAnalyse = function() {
			FwaTabsetService.selectTab('settleAnalyse.regionView',{
				indicator: $stateParams.indicator,
				startDate: $scope.date.start,
				endDate: $scope.date.end
			},false);
		}
		$(window).on('resize',function(){
			setTimeout(function(){
				pocketCostsOption();
				totalCostOption();
			},0);
		});
		
	}]);
	
})();